package org.jeecg.modules.friends.fetch.youzan;

import static org.jeecg.modules.friends.fetch.youzan.YouzanTokenFetch.clientId;
import static org.jeecg.modules.friends.fetch.youzan.YouzanTokenFetch.clientSecret;
import static org.jeecg.modules.friends.fetch.youzan.YouzanTokenFetch.ktdId;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youzan.cloud.open.sdk.common.exception.SDKException;
import com.youzan.cloud.open.sdk.core.client.auth.Token;
import com.youzan.cloud.open.sdk.core.client.core.DefaultYZClient;
import com.youzan.cloud.open.sdk.core.client.core.YouZanClient;
import com.youzan.cloud.open.sdk.gen.v1_0_0.api.YouzanCloudSecretDecryptSingle;
import com.youzan.cloud.open.sdk.gen.v1_0_0.model.YouzanCloudSecretDecryptSingleParams;
import com.youzan.cloud.open.sdk.gen.v1_0_0.model.YouzanCloudSecretDecryptSingleResult;
import com.youzan.cloud.open.security.SecretClient;
import com.youzan.cloud.open.security.exception.DataSecurityException;
import com.youzan.cloud.open.security.mask.MaskType;

import lombok.extern.slf4j.Slf4j;

/**
 * @author cold
 * @data 2024/6/21 21:12
 * @version 1.0
 */
@Service
@Slf4j
public class YouzanOrderFetch {

    @Autowired
    private YouzanTokenFetch youzanTokenFetch;

    public static final SecretClient secretClient;

    static {
        try {
            secretClient = new SecretClient(clientId,clientSecret);
        } catch (DataSecurityException e) {
            throw new RuntimeException(e);
        }
    }

    private static final YouZanClient yzClient = new DefaultYZClient();

    public String phoneDecrypt(String encryptPhone) {
        String tokenStr = youzanTokenFetch.getYouZToken();
        Token token = new Token(tokenStr);
        YouzanCloudSecretDecryptSingle youzanCloudSecretDecryptSingle = new YouzanCloudSecretDecryptSingle();
        YouzanCloudSecretDecryptSingleParams youzanCloudSecretDecryptSingleParams = new YouzanCloudSecretDecryptSingleParams();
        youzanCloudSecretDecryptSingleParams.setSource(encryptPhone);
        youzanCloudSecretDecryptSingle.setAPIParams(youzanCloudSecretDecryptSingleParams);
        try {
            YouzanCloudSecretDecryptSingleResult result = yzClient.invoke(youzanCloudSecretDecryptSingle, token, YouzanCloudSecretDecryptSingleResult.class);
            if (result.getCode() == 200) {
                return result.getData();
            } else {
                log.info("解密失败");
                return "";
            }
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    public String phoneDecryptClient(String encryptPhone) {
        try {
            return secretClient.decrypt(Long.valueOf(ktdId), encryptPhone);
        } catch (DataSecurityException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        try {
            System.out.println(secretClient.decrypt(113214832L, "$CJS32qfwn9HXlm0tBWccvw==$1$"));
        } catch (DataSecurityException e) {
            throw new RuntimeException(e);
        }
//        Token token = new Token("71ccc84b741d0708cf3d7a28ee5e82d");
//        YouzanCloudSecretDecryptSingle youzanCloudSecretDecryptSingle = new YouzanCloudSecretDecryptSingle();
//        YouzanCloudSecretDecryptSingleParams youzanCloudSecretDecryptSingleParams = new YouzanCloudSecretDecryptSingleParams();
//        youzanCloudSecretDecryptSingleParams.setSource("$CJS32qfwn9HXlm0tBWccvw==$1$");
//        youzanCloudSecretDecryptSingle.setAPIParams(youzanCloudSecretDecryptSingleParams);
//        try {
//            YouzanCloudSecretDecryptSingleResult result = yzClient.invoke(youzanCloudSecretDecryptSingle, token, YouzanCloudSecretDecryptSingleResult.class);
//            if (result.getCode() == 200) {
//                System.out.println(result.getData());
//            } else {
//                log.info("解密失败");
//            }
//        } catch (SDKException e) {
//            throw new RuntimeException(e);
//        }
    }
}
